package com.lq.ecoldchain.base.view

import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.os.Build
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.view.*
import com.lq.ecoldchain.R
import com.lq.ecoldchain.base.viewdelegate.IViewDelegate
import com.lq.ecoldchain.callback.LifeCallBack
import com.lq.ecoldchain.callback.SafeClickListener
import com.lq.ecoldchain.util.ServiceUtil
import kotlinx.android.synthetic.main.activity_base.*
import kotlinx.android.synthetic.main.tool_title.*

/*
*2018/12/21 0021  10:17
*function by lq
*/
abstract class BaseActivity<B : ViewDataBinding> : BaseInjectActivity() {
    protected lateinit var binding: B
    private lateinit var mContentView: ViewGroup

    abstract val layoutId: Int

    abstract val viewDelegate: IViewDelegate
    abstract fun initView()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        super.setContentView(R.layout.activity_base)
        mContentView = findViewById(android.R.id.content)
        obtainContentView()
        lifecycle.addObserver(viewDelegate)
        initBinding()
        setStatusBlackFont()
        setSupportActionBar(mToolbar)
        supportActionBar?.setDisplayShowTitleEnabled(false)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        initView()

        ServiceUtil.startLocationService(this)
    }

    private fun initBinding() {
        binding = DataBindingUtil.setContentView(this, layoutId)
        with(binding) {
            setLifecycleOwner(this@BaseActivity)
        }
    }

    private fun setStatusBlackFont() {
        if (Build.VERSION.SDK_INT >= 21) {
            val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            window.decorView.systemUiVisibility = option
        }
    }

    private fun obtainContentView() {
        if (contentRl != null) {
            val view = LayoutInflater.from(this).inflate(layoutId, contentRl, false)
            contentRl.id = android.R.id.content
            mContentView.id = View.NO_ID
            contentRl.removeAllViews()
            contentRl.addView(view)
        }
    }

    protected fun titleText(title: String) {
        titleTv.text = title
    }

    protected fun rightIcon(icon: Int?, listener: SafeClickListener) {
        if (icon != null) {
            titleRightIconIv.setImageResource(icon)
        }
        titleRightIconIv.visibility = View.VISIBLE
        rightTextTv.visibility = View.GONE
        titleRightIconIv.setOnClickListener(listener)
    }

    protected fun rightText(text: String, listener: SafeClickListener) {
        rightTextTv.text = text
        rightTextTv.visibility = View.VISIBLE
        rightTextTv.setOnClickListener(listener)
        titleRightIconIv.visibility = View.GONE
    }

    protected fun titleGone() {
        titleRl.visibility = View.GONE
    }

    protected fun nonFitSystem(){
        baseLl.fitsSystemWindows = false
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            LifeCallBack.remove(this)
            finish()
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item?.itemId == android.R.id.home) {
            LifeCallBack.remove(this)
            finish()
            return true
        }
        return super.onOptionsItemSelected(item)
    }
}
